package com.xihuanjun.healthcode.entity;

import com.xihuanjun.healthcode.exception.ServiceException;
import com.xihuanjun.util.MapUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author: 江溪浣
 * @createDate: 2022/10/25 13:32
 * @description:
 */
@Data
@Slf4j
@Component
@Order(1)
@ConfigurationProperties(prefix = "xihuanjun.types")
public class AllTypes {
    private String cardTypes;
    private String personTypes1;
    private String personTypes2;
    private String personTypes3;

    public static Map<Integer,String> cardType=new HashMap();
    public static Map<Integer,String> personType1=new HashMap();
    public static Map<Integer,String> personType2=new HashMap();
    public static Map<Integer,String> personType3=new HashMap();


    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        System.out.println("证件类型："+this.cardTypes);
        System.out.println("证件类型："+this.personTypes1);
        System.out.println("证件类型："+this.personTypes2);
        System.out.println("证件类型："+this.personTypes3);
        String[] cardTypesStr = this.cardTypes.split(",");
        for (int i = 0; i < cardTypesStr.length; i++) {
            cardType.put(i+1,cardTypesStr[i]);
        }
        String[] personTypesStr = this.personTypes1.split(",");
        for (int i = 0; i < personTypesStr.length; i++) {
            personType1.put(i+1,personTypesStr[i]);
        }
        String[] personTypes2Str = this.personTypes2.split(",");
        for (int i = 0; i < personTypes2Str.length; i++) {
            personType2.put(i+1,personTypes2Str[i]);
        }
        String[] personTypes3Str = this.personTypes3.split(",");
        for (int i = 0; i < personTypes3Str.length; i++) {
            personType3.put(i+1,personTypes3Str[i]);
        }
        log.debug(personType1.toString());
        log.debug(personType2.toString());
        log.debug(personType2.toString());
    }

    public static String getCardTypeName(int id){
        return cardType.get(id).toString();
    }


    public static String getPersonTypeName(int id,int changeType){
        switch (changeType)
        {
            case 1:
                return personType1.get(id).toString();
            case 2:
                return personType2.get(id).toString();
            case 3:
                return personType3.get(id).toString();
            default:
                throw new ServiceException("获取类型Id出错，赋码类型错误，应该为1/2/3");
        }
    }

    public static int getCardTypeId(String typeName){
        Integer key = MapUtils.getKeyByValue(cardType, typeName);
        if(key==null){
            throw new ServiceException(MessageFormat.format("找不到类型【{0}】对应的key",typeName));
        }
        return key.intValue();
    }

    public static int getPersonTypeId(String typeName,int changeType){
        Integer key=null;
        switch (changeType)
        {
            case 1:
                key = MapUtils.getKeyByValue(personType1, typeName);
            break;

            case 2:
                key = MapUtils.getKeyByValue(personType2, typeName);
            break;
            case 3:
                key = MapUtils.getKeyByValue(personType3, typeName);
            break;
            default:
                throw new ServiceException("获取类型Id出错，赋码类型错误，应该为1/2/3");
        }
        if(key==null){
            throw new ServiceException(MessageFormat.format("找不到类型【{0}】对应的key",typeName));
        }
        return key.intValue();
    }


}
